package com.jrx.anytxn.reporting.job.unionFile.config;

import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.reporting.config.AnyTxnBatchProperties;
import com.jrx.anytxn.reporting.constant.ReportingConstant;
import com.jrx.anytxn.reporting.job.unionFile.step.RisManagementFileListener;
import com.jrx.anytxn.reporting.job.unionFile.step.RisManagementFileProcessor;
import com.jrx.anytxn.reporting.job.unionFile.step.RisManagementFileReader;
import com.jrx.anytxn.reporting.job.unionFile.step.RisManagementFileWriter;
import com.jrx.anytxn.transaction.entity.TlLoanClassifyChange;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisPagingItemReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import javax.annotation.Resource;

/**
 * 生成风控信息文件(按照业务日期 增量抽取)
 * lbz
 */
@Configuration
@EnableConfigurationProperties(AnyTxnBatchProperties.class)
@EnableBatchProcessing
public class GenerateRisManagementFileConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(GenerateRisManagementFileConfiguration.class);

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private AnyTxnBatchProperties anyTxnBatchProperties;
    @Autowired
    private IOrganizationTableService organizationTableService;
    @Autowired
    private RisManagementFileListener risManagementFileListener;
    @Autowired
    private RisManagementFileProcessor risManagementFileProcessor;
    @Autowired
    private RisManagementFileWriter risManagementFileWriter;


    @Bean
    public Job generateRisManagementFileJob(){
        return jobBuilderFactory.get("generateRisManagementFileJob").incrementer(new RunIdIncrementer()).listener(risManagementFileListener)
                .start(generateRisManagementFileStep())
                .build();
    }

    @Bean
    public Step generateRisManagementFileStep(){
        return stepBuilderFactory.get("generateRisManagementFileStep")
                .<TlLoanClassifyChange, String>chunk(anyTxnBatchProperties.getFileConfig().getChunkLimit())
                .reader(risManagementFileReader())
                .processor(risManagementFileProcessor)
                .writer(risManagementFileWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anyTxnBatchProperties.getFileConfig().getThrottleLimit())
                .build();
    }

    @Bean
    @StepScope
    public MyBatisPagingItemReader<TlLoanClassifyChange> risManagementFileReader() {
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(ReportingConstant.DEFAULT_ORG_NUMBER);
        return new RisManagementFileReader(sqlSessionFactory, org,anyTxnBatchProperties.getFileConfig().getPageSize());
    }
}
